﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Support.
    /// </summary>
    internal class SupportDAO
    {

        #region general

        private static Support LoadObj(IDataReader datareader)
        {
            Support obj = new Support();
            obj.SupportID = MpDataHelp.GetInt(datareader, "SupportID");
            obj.Yahoo = MpDataHelp.GetString(datareader, "Yahoo");
            obj.Skyper = MpDataHelp.GetString(datareader, "Skyper");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.Type = MpDataHelp.GetInt(datareader, "Type");

            return obj;
        }
        #endregion

        public static int Support_Add(Support obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Support_Add");
            db.AddOutParameter(dbCommand, "SupportID", DbType.Int32, obj.SupportID);
            db.AddInParameter(dbCommand, "Yahoo", DbType.String, obj.Yahoo);
            db.AddInParameter(dbCommand, "Skyper", DbType.String, obj.Skyper);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, obj.Type);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@SupportID"));
            return -1;
        }
        public static int Support_Update(Support obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Support_Update");
            db.AddInParameter(dbCommand, "SupportID", DbType.Int32, obj.SupportID);
            db.AddInParameter(dbCommand, "Yahoo", DbType.String, obj.Yahoo);
            db.AddInParameter(dbCommand, "Skyper", DbType.String, obj.Skyper);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, obj.Type);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Support_Delete(Int32 SupportID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Support_Delete");
            db.AddInParameter(dbCommand, "SupportID", DbType.Int32, SupportID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Support> Support_List(int SupportID, int Type, int IsActive, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Support_List");
            db.AddInParameter(dbCommand, "SupportID", DbType.Int32, SupportID);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, Type);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<Support> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Support>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
